package com.hiof.overlays;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.hiof.Configuration;
import com.hiof.R;
import com.hiof.servercommunication.background.job.listener.IAthletesLatestLocationReceiveListener;
import com.hiof.servercommunication.receive.beans.AthleteLocationReceivePacket;
import com.hiof.servercommunication.receive.beans.MemberLocationInfo;
import com.hiof.servercommunication.receive.beans.Packet;
import com.hiof.servercommunication.receive.beans.TeamMemberLocationPacket;
import com.hiof.utility.Utility;

public class AthletePositionOverlay extends Overlay implements IAthletesLatestLocationReceiveListener {
    Resources resources;
    Bitmap bmp;
    AthleteLocationReceivePacket athleteLocationReceivePacket;
    public AthletePositionOverlay(Resources resources) {
        this.resources = resources;
        athleteLocationReceivePacket = new AthleteLocationReceivePacket();
    }


    @Override
    public void draw(Canvas canvas, MapView mapView, boolean b) {
        super.draw(canvas, mapView, false);
        drawTeammatesLocation(canvas, mapView);
    }

    private void drawTeammatesLocation(Canvas canvas, MapView mapView) {
        int markerID = R.drawable.cycling;
        Point screenPt = new Point();
        if(athleteLocationReceivePacket!=null){
            GeoPoint point = new GeoPoint(Math.round((int)(athleteLocationReceivePacket.getLat()*1e6)), Math.round((int)(athleteLocationReceivePacket.getLng()*1e6)));
            mapView.getProjection().toPixels(point, screenPt);
        }
        drawMarker(canvas, markerID, resources, screenPt.x, screenPt.y);
    }

    public static void drawMarker(Canvas canvas, int id, Resources resources,
                                  int x, int y) {
        Bitmap bitmap = BitmapFactory.decodeResource(resources, id);
        canvas.drawBitmap(bitmap, x - 10, y - 10, null);
    }

    @Override
    public void onListen(Packet packet) {
        if (packet instanceof AthleteLocationReceivePacket) {
            this.athleteLocationReceivePacket = (AthleteLocationReceivePacket) packet;
            Utility.debug(this.getClass().getName(), "location update for athlete received:"+athleteLocationReceivePacket.getLat());
            Toast.makeText(Configuration.getInstance().getCurrentContext(), "athelte overlay:"+ athleteLocationReceivePacket.getLat(), Toast.LENGTH_SHORT).show();
        }
    }
}
